package cn.iocoder.yudao.module.student.controller.app.newsStudyInfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.NewsStudyInfoApi;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyInfoDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyInfoPageDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyRankingDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyTypeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 新闻(研究成果) API 接口
 *
 */
@Api(tags = "管理后台 -学生端-  新闻(研究成果)")
@RestController
@RequestMapping("/student/new-study-info")
@Validated
public class WebNewsStudyInfoController {
    @Resource
    private NewsStudyInfoApi newsStudyInfoApi;


    @GetMapping("/getStudentNewsStudyType")
    @ApiOperation("获取新闻栏目(成果分类)")
    @ApiImplicitParam(name = "type", value = "类型:1:新闻栏目2:成果类型", required = true, example = "1", dataTypeClass = Integer.class)
    public CommonResult<List<NewsStudyTypeDTO>> getStudentNewsStudyType(@RequestParam("type") Integer type) {
        List<NewsStudyTypeDTO> newsStudyTypeList = newsStudyInfoApi.getNewsStudyTypeList(type);
        return success(newsStudyTypeList);
    }


    @GetMapping("/geStudentNewsStudyInfoPage")
    @ApiOperation("获取新闻信息(研究成果)分页")
    public CommonResult<PageResult<NewsStudyInfoDTO>> geStudentNewsStudyInfoPage(@Valid NewsStudyInfoPageDTO pageVO){
        pageVO.setQueryDetail("1");
        pageVO.setStatus("1");
        if(  null==pageVO.getSortField()){
            List<SortingField> list =new ArrayList<>();
            SortingField sf=new SortingField();
            sf.setField("t.is_top");
            sf.setOrder("desc");
            list.add(sf);
            sf=new SortingField();
            sf.setField("t.release_time");
            sf.setOrder("desc");
            list.add(sf);
            pageVO.setSortingFields(list);
        }
        PageResult<NewsStudyInfoDTO> newsStudyInfoPage = newsStudyInfoApi.getNewsStudyInfoPage(pageVO);
        return success(newsStudyInfoPage);
    }


    @GetMapping("/getStudentNewsStudyInfoById")
    @ApiOperation("获取新闻信息(研究成果)详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<NewsStudyInfoDTO> getStudentNewsStudyInfoById(@RequestParam("id") Long id) {
        NewsStudyInfoDTO newsStudyInfo= newsStudyInfoApi.getNewsStudyInfoById(id);
        newsStudyInfoApi.updateViewNum(id);
        return success(newsStudyInfo);
    }

    @GetMapping("/updateViewNum")
    @ApiOperation("更新点击量")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<Boolean> updateViewNum(@RequestParam("id") Long id) {
        newsStudyInfoApi.updateViewNum(id);
        return success(true);
    }



    @GetMapping("/getStudentNewsStudyRanking")
    @ApiOperation("获取周榜月榜")
    @ApiImplicitParam(name = "type", value = "类型:1:周榜,2:月榜", required = true, example = "1", dataTypeClass = Integer.class)
    public CommonResult<List<NewsStudyRankingDTO>> getStudentNewsStudyRanking(@RequestParam("type") Integer type) {
        List<NewsStudyRankingDTO> list = newsStudyInfoApi.getNewsStudyRanking(type);
        return success(list);
    }
}
